/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.babble.manage.service.impl;

import com.babble.manage.conversion.ImgCategoryConversion;
import com.babble.manage.dto.ImgCategoryDTO;
import com.babble.manage.dto.QueryImgCategoryDTO;
import com.babble.manage.entity.ImgCategory;
import com.babble.manage.mapper.ImgCategoryMapper;
import com.babble.manage.service.IImgCategoryService;
import com.babble.manage.vo.ImgCategoryVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

/**
 * 图片分类表 服务实现类
 *
 * @author Blade
 * @since 2021-11-21
 */
@RequiredArgsConstructor
@Service
public class ImgCategoryServiceImpl extends ServiceImpl<ImgCategoryMapper, ImgCategory> implements IImgCategoryService {
	private final ImgCategoryConversion imgCategoryConversion;

	@Override
	public IPage<ImgCategoryVO> selectImgCategoryPage(IPage<ImgCategoryVO> page, ImgCategoryVO imgCategory) {
		return page.setRecords(baseMapper.selectImgCategoryPage(page, imgCategory));
	}

	@Override
	public ImgCategory create(ImgCategoryDTO categoryDTO) {
		ImgCategory imgCategory = imgCategoryConversion.imgCategoryDtoToImgCategory(categoryDTO);
		this.save(imgCategory);
		return imgCategory;
	}

	@Override
	public IPage<ImgCategory> list(QueryImgCategoryDTO queryImgCategoryDTO) {
		LambdaQueryWrapper<ImgCategory> wrapper = new LambdaQueryWrapper<>();

		//模糊匹配图片分类名称
		if (Func.isNotEmpty(queryImgCategoryDTO.getName())) {
			wrapper.like(ImgCategory::getName, queryImgCategoryDTO.getName());
		}
		if (queryImgCategoryDTO.getSort() != null && queryImgCategoryDTO.getSort()) {
			wrapper.orderByDesc(ImgCategory::getSort);
		} else {
			wrapper.orderByAsc(ImgCategory::getSort);
		}
		return this.page(new Page<>(queryImgCategoryDTO.getCurrent(), queryImgCategoryDTO.getSize()), wrapper);
	}

	@Override
	public ImgCategory update(String id, ImgCategoryDTO categoryDTO) {
		ImgCategory imgCategory = imgCategoryConversion.imgCategoryDtoToImgCategory(categoryDTO);
		imgCategory.setId(id);
		this.updateById(imgCategory);
		return imgCategory;
	}

	@Override
	public void delete(String id) {
		this.removeById(id);
		//TODO wm (不要动)考虑是否删除整个分类的图片
	}
}
